package com.pe1n.yangtaopai.page.detail.view.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.jude.rollviewpager.RollPagerView;
import com.pe1n.yangtaopai.R;
import com.pe1n.yangtaopai.adapter.MyAboutAdapter;
import com.pe1n.yangtaopai.adapter.MyGoodsViewPagerAdapter;
import com.pe1n.yangtaopai.adapter.MyImgRecyclerAdapter;
import com.pe1n.yangtaopai.adapter.MyMultiTitleAdapter;
import com.pe1n.yangtaopai.bean.BigCarBean;
import com.pe1n.yangtaopai.bean.CarBean;
import com.pe1n.yangtaopai.bean.CarBeanHelper;
import com.pe1n.yangtaopai.bean.CollectBean;
import com.pe1n.yangtaopai.bean.CollectBeanHelper;
import com.pe1n.yangtaopai.bean.DetailBean;
import com.pe1n.yangtaopai.page.detail.presenter.PresenterImpl;
import com.pe1n.yangtaopai.page.detail.view.interfacese.IView;
import com.pe1n.yangtaopai.utils.URLConstant;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

public class DetailActivity extends AppCompatActivity implements IView {

    private Context mContext = this;
    private ProgressBar progressBar;
    private PresenterImpl mPresenter;
    private DetailBean bean;
    private List<String> mTitleList;
    private List<String> mImgList;
    private List<DetailBean.DataBeanX.RelatedProdBean> mRelatedProdList;
    private RollPagerView rollPagerViewGoods;
    private ImageView imageViewGoods;
    private TextView textViewGoodsName;
    private TextView textViewGoodsPrice;
    private TextView textViewGoodsPriceJpn;
    private TextView textViewChoice;
    private TextView textViewGoodsBrand;
    private TextView textViewSpecial;
    private TextView textViewTitle;
    private ImageView imageViewBack;
    private ImageView imageViewShengming1;
    private ImageView imageViewShengming2;
    private ImageView imageViewShengming3;
    private ImageView imageViewShengming4;
    private TextView textViewShengming1;
    private TextView textViewShengming2;
    private TextView textViewShengming3;
    private TextView textViewShengming4;
    private RecyclerView recyclerViewRelated;
    private RecyclerView recyclerViewGoodsBottom;
    private ScrollView scrollViewTop,scrollViewBottom;
    private TextView mTextViewCar;//加入购物车
    private SharedPreferences mSharePreferrnces;
    private String stringName;//用户的名字
    private String url;
    private CarBean carBean;
    private CollectBean collectBean;//收藏
    private LinearLayout mLinearLayoutCollect;//收藏
    private int id;
    private ImageView mImageViewCollect;
    private CollectBeanHelper collectBeanHelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);

        initView();

        initData();


    }

    @Override
    protected void onStart() {
        super.onStart();

    }

    @Override
    protected void onStop() {
        super.onStop();

    }





    private void initData() {

         Intent intent = getIntent();
         id=intent.getIntExtra("id", 5411);
         url = String.format(URLConstant.DETAIL_URL, id);
        collectBeanHelper=new CollectBeanHelper();
        String name = intent.getStringExtra("name");
        textViewTitle.setText(name);
        //Log.e("tag", url);
        mPresenter = new PresenterImpl(this);
        mPresenter.loadDetailBeanData(url);

    }

    private void initView() {
        carBean = new CarBean();
        collectBean=new CollectBean();
        mImageViewCollect= (ImageView) findViewById(R.id.activity_detail_collect_imageView);
        mSharePreferrnces=getSharedPreferences("myshared",0);
        stringName=mSharePreferrnces.getString("shareUserName","no");
        textViewTitle = (TextView) findViewById(R.id.tv_title_detail);
        progressBar = (ProgressBar) findViewById(R.id.progress_bar_detail);
        imageViewGoods = (ImageView) findViewById(R.id.iv_goods_detail);
        rollPagerViewGoods = (RollPagerView) findViewById(R.id.roll_pager_view_goods);
        //rollPagerViewGoods.setHintView();
        textViewGoodsName = (TextView) findViewById(R.id.tv_goods_name);
        textViewGoodsPrice = (TextView) findViewById(R.id.tv_goods_price);
        textViewGoodsPriceJpn = (TextView) findViewById(R.id.tv_goods_price_jpn);
        textViewGoodsBrand = (TextView) findViewById(R.id.tv_goods_brand);
        textViewChoice = (TextView) findViewById(R.id.tv_choice);
        imageViewBack = (ImageView) findViewById(R.id.iv_back_detail);
        imageViewBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        textViewSpecial = (TextView) findViewById(R.id.tv_msg);
        mLinearLayoutCollect= (LinearLayout) findViewById(R.id.favorite_layout);
        imageViewShengming1 = (ImageView) findViewById(R.id.iv_shengming1);
        imageViewShengming2 = (ImageView) findViewById(R.id.iv_shengming2);
        imageViewShengming3 = (ImageView) findViewById(R.id.iv_shengming3);
        imageViewShengming4 = (ImageView) findViewById(R.id.iv_shengming4);
        textViewShengming1 = (TextView) findViewById(R.id.tv_shengming1);
        textViewShengming2 = (TextView) findViewById(R.id.tv_shengming2);
        textViewShengming3 = (TextView) findViewById(R.id.tv_shengming3);
        textViewShengming4 = (TextView) findViewById(R.id.tv_shengming4);
        recyclerViewRelated = (RecyclerView) findViewById(R.id.recycler_view_goods);
        recyclerViewGoodsBottom = (RecyclerView) findViewById(R.id.recycler_view_goods_bottom);
        scrollViewTop = (ScrollView) findViewById(R.id.scrollViewTop);
        scrollViewBottom = (ScrollView) findViewById(R.id.scrollViewBottom);
        mTextViewCar= (TextView) findViewById(R.id.add_to_buy_car);
        recyclerViewRelated.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {


                return false;
            }
        });
    }

    @Override
    public void showProgress() {

    }

    @Override
    public void hideProgress() {
        progressBar.setVisibility(View.GONE);
    }

    @Override
    public void showLoadFailMsg() {

    }

    @Override
    public void loadDetailBeanData(DetailBean detailBean) {
        bean = detailBean;
        mTitleList = bean.getData().getPicUrls();
        mImgList = bean.getData().getDescpPic();
        mRelatedProdList = bean.getData().getRelatedProd();
        loadData();
    }

    private void loadData() {

        Log.e("tag", bean.getData().getCName());
        if (mTitleList.size() > 1) {
            imageViewGoods.setVisibility(View.GONE);
            rollPagerViewGoods.setAdapter(new MyGoodsViewPagerAdapter(mContext,mTitleList,rollPagerViewGoods));
            rollPagerViewGoods.getViewPager().getAdapter().notifyDataSetChanged();
        } else {
            rollPagerViewGoods.setVisibility(View.GONE);
            Glide.with(mContext).load(bean.getData().getPicUrls().get(0)).placeholder(R.drawable.place_holder).into(imageViewGoods);
        }
        textViewGoodsName.setText(bean.getData().getCName());
        textViewGoodsPrice.setText(getResources().getString(R.string.symbol) + bean.getData().getReferencePrice());
        textViewGoodsPriceJpn.setText("(" + bean.getData().getJpPrice() + "円)");
        textViewGoodsBrand.setText(bean.getData().getChannelInfo().getCName());
        textViewChoice.setText(bean.getData().getSpec1Val());
        textViewSpecial.setText(bean.getData().getDescpT());

        //收藏的监听
        mLinearLayoutCollect.setOnClickListener(new View.OnClickListener() {
            //收藏
            @Override
            public void onClick(View v) {
                mImageViewCollect.setImageResource(R.drawable.hongxin);
                if(bean!=null){
                    if(!stringName.equals("no")) {
                        collectBean.setName(bean.getData().getCName());
                        collectBean.setInfoUrl(url);
                        collectBean.setPic((bean.getData().getPicUrls()).get(0));
                        collectBean.setPrice(""+bean.getData().getReferencePrice());
                        collectBean.setId(id);
                        collectBean.setNameUserName(stringName);
                        collectBean.save(new SaveListener<String>() {
                            @Override
                            public void done(String s, BmobException e) {
                                if(e==null){
                                    Toast.makeText(mContext,"收藏成功!",Toast.LENGTH_LONG).show();
                                    collectBeanHelper.setString(stringName);
                                    EventBus.getDefault().postSticky(collectBeanHelper);
                                }
                                else{
                                    Toast.makeText(mContext,"收藏失败!",Toast.LENGTH_LONG).show();
                                }
                            }
                        });

                    }
                    else{
                        Toast.makeText(mContext,"您还没有登录",Toast.LENGTH_SHORT).show();
                    }
                }

            }
        });
        Glide.with(mContext).load(bean.getData().getStatement().get(0).getPicUrl()).placeholder(R.drawable.place_holder).into(imageViewShengming1);
        Glide.with(mContext).load(bean.getData().getStatement().get(1).getPicUrl()).placeholder(R.drawable.place_holder).into(imageViewShengming2);
        Glide.with(mContext).load(bean.getData().getStatement().get(2).getPicUrl()).placeholder(R.drawable.place_holder).into(imageViewShengming3);
        Glide.with(mContext).load(bean.getData().getStatement().get(3).getPicUrl()).placeholder(R.drawable.place_holder).into(imageViewShengming4);
        textViewShengming1.setText(bean.getData().getStatement().get(0).getTitle());
        textViewShengming2.setText(bean.getData().getStatement().get(1).getTitle());
        textViewShengming3.setText(bean.getData().getStatement().get(2).getTitle());
        textViewShengming4.setText(bean.getData().getStatement().get(3).getTitle());
        recyclerViewRelated.setAdapter(new MyAboutAdapter(mContext,mRelatedProdList));
        recyclerViewRelated.setLayoutManager(new GridLayoutManager(mContext,3, OrientationHelper.VERTICAL,false));
        recyclerViewGoodsBottom.setAdapter(new MyImgRecyclerAdapter(mContext,mImgList));
        recyclerViewGoodsBottom.setLayoutManager(new LinearLayoutManager(mContext,OrientationHelper.VERTICAL,false));
        mTextViewCar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(bean!=null) {
                    if(!stringName.equals("no")) {
                        carBean.setName(bean.getData().getCName());
                        carBean.setWeight(bean.getData().getSpec1Val());
                        carBean.setUserName(stringName);
                        final CarBeanHelper carBeanHelper=new CarBeanHelper();
                        carBeanHelper.setName(stringName);
                        carBean.setAmount(1);
                        carBean.setId(id);
                        carBean.setMoney(((Double) bean.getData().getReferencePrice()));
                        carBean.setImageViewUrl((bean.getData().getPicUrls()).get(0));
                        carBean.setPicInfo( url );
                        carBean.save(new SaveListener<String>() {
                            @Override
                            public void done(String s, BmobException e) {
                                if(e==null){
                                    Toast.makeText(mContext,"添加购物车成功!",Toast.LENGTH_LONG).show();
                                    EventBus.getDefault().postSticky(carBeanHelper);
                                }
                                else{
                                    Toast.makeText(mContext,"添加购物车失败!",Toast.LENGTH_LONG).show();
                                }
                            }
                        });
                    }
                    else{
                        Toast.makeText(mContext,"你还没有登录!",Toast.LENGTH_LONG).show();
                    }

                }
            }
        });
    }

}
